package net.maku.association.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import net.maku.association.dao.InterestAssociationDao;
import net.maku.association.entity.InterestAssociationEntity;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
import net.maku.association.convert.InterestAssociationDynamicsConvert;
import net.maku.association.entity.InterestAssociationDynamicsEntity;
import net.maku.association.query.InterestAssociationDynamicsQuery;
import net.maku.association.vo.InterestAssociationDynamicsVO;
import net.maku.association.dao.InterestAssociationDynamicsDao;
import net.maku.association.service.InterestAssociationDynamicsService;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 协会动态
 *
 * @author 阿沐 babamu@126.com
 * @since 1.0.0 2025-04-26
 */
@Service
@AllArgsConstructor
public class InterestAssociationDynamicsServiceImpl extends BaseServiceImpl<InterestAssociationDynamicsDao, InterestAssociationDynamicsEntity> implements InterestAssociationDynamicsService {

    private final InterestAssociationDao interestAssociationDao;

    @Override
    public PageResult<InterestAssociationDynamicsVO> page(InterestAssociationDynamicsQuery query) {
        IPage<InterestAssociationDynamicsEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));
        List<InterestAssociationDynamicsVO> list = InterestAssociationDynamicsConvert.INSTANCE.convertList(page.getRecords());
        Long associationId = query.getAssociationId();
        if(associationId != null){
            InterestAssociationEntity interestAssociationEntity = interestAssociationDao.selectById(associationId);
            list.forEach(l->{
                l.setName(interestAssociationEntity.getName());
            });
        }

        return new PageResult<>(list, page.getTotal());
    }

    private LambdaQueryWrapper<InterestAssociationDynamicsEntity> getWrapper(InterestAssociationDynamicsQuery query){
        LambdaQueryWrapper<InterestAssociationDynamicsEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(query.getAssociationId() != null,InterestAssociationDynamicsEntity::getAssociationId,query.getAssociationId())
                .eq(StringUtils.isNotBlank(query.getTitle()),InterestAssociationDynamicsEntity::getTitle,query.getTitle());
        return wrapper;
    }

    @Override
    public void save(InterestAssociationDynamicsVO vo) {
        InterestAssociationDynamicsEntity entity = InterestAssociationDynamicsConvert.INSTANCE.convert(vo);
        entity.setCreateTime(new Date());
        baseMapper.insert(entity);
    }

    @Override
    public void update(InterestAssociationDynamicsVO vo) {
        InterestAssociationDynamicsEntity entity = InterestAssociationDynamicsConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

}